
from scrapy.contrib.pipeline import images
from scrapy.contrib.pipeline.images import *


class DownloadImagePipeline(ImagesPipeline):

    def file_path(self, request, response=None, info=None):
        ## start of deprecation warning block (can be removed in the future)
        def _warn():
            from scrapy.exceptions import ScrapyDeprecationWarning
            import warnings
            warnings.warn('ImagesPipeline.image_key(url) and file_key(url) methods are deprecated, '
                          'please use file_path(request, response=None, info=None) instead',
                          category=ScrapyDeprecationWarning, stacklevel=1)

        # check if called from image_key or file_key with url as first argument
        if not isinstance(request, Request):
            _warn()
            url = request
        else:
            url = request.url

        # detect if file_key() or image_key() methods have been overridden
        if not hasattr(self.file_key, '_base'):
            _warn()
            return self.file_key(url)
        elif not hasattr(self.image_key, '_base'):
            _warn()
            return self.image_key(url)
        ## end of deprecation warning block

        return 'full/%s.jpg' % (self.file_name)


    def item_completed(self, results, item, info):
        self.file_name = item['title']
        if isinstance(item, dict) or self.images_result_field in item.fields:
            item[self.images_result_field] = [x for ok, x in results if ok]
        return item


class GetImgSrcPipeline(object):

    def __init__(self):
        self.file = open('data.txt','w')

    def process_item(self, item, spider):
        images.ImagesPipeline.process_item(item,spider)
        if item['title']:
            line = "" + item['title'][0] + "@" +item['src'][0] + '\n'
            print("********************************************")
            print(line)
            print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
            self.file.write(line)
            return item
        else:
            print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
            raise DropItem("no title item missed")
